package com.wx.fresh.common.util;


import com.wx.fresh.common.enums.IErrorCode;
import com.wx.fresh.common.enums.ResultCode;

/**
 * 通用返回对象
 * Created by wx.mall on 2019/4/19.
 */
public class CommonResult<T> {
    private static final Object LOG_BACK_IN = "/html/login.html";
    private String msg;
    private Integer code = 0;
    private Integer count;
    private Long total;
    private T data;

    protected CommonResult() {
    }

    protected CommonResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功返回结果
     *
     * @param获取的数据
     */
    public static <T> CommonResult<T> success() {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
    }
    
    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> success(T data) {
        return data == null ? new com.wx.fresh.common.util.CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null) : new com.wx.fresh.common.util.CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    /**
     * 文件上传返回
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> upload(T data) {
        return getSingtenModel(ResultCode.UPLOAD_SUCCESS.getCode(), ResultCode.UPLOAD_SUCCESS.getMsg(), ResultCode.UPLOAD_SUCCESS.getCount(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> success(T data, String message) {
        return new com.wx.fresh.common.util.CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> failed(IErrorCode errorCode) {
        return new com.wx.fresh.common.util.CommonResult<T>(errorCode.getCode(), errorCode.getMsg(), null);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> failed(String message) {
        return new com.wx.fresh.common.util.CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }


    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> validateFailed(String message) {
        return new com.wx.fresh.common.util.CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> unauthorized(T data) {
        return new com.wx.fresh.common.util.CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMsg(), data);
    }

    /**
     * 未授权返回结果
     *
     * @return
     */
    public static <T> com.wx.fresh.common.util.CommonResult<T> forbidden(T data) {
        return new com.wx.fresh.common.util.CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMsg(), data);
    }

    /**
     * 用户名错误
     *
     * @return
     */
    public static com.wx.fresh.common.util.CommonResult userNameERROR() {
        com.wx.fresh.common.util.CommonResult resultBean = new com.wx.fresh.common.util.CommonResult();
        resultBean.setData(null);
        resultBean.setMsg(ResultCode.DUPLICATE_USER_NAME.getMsg());
        return resultBean;
    }

    /**
     * 获取的信息
     *
     * @param code
     * @param msg
     * @param count
     * @param data
     * @return
     */
    private static com.wx.fresh.common.util.CommonResult getSingtenModel(Integer code, String msg, Integer count, Object data) {
        com.wx.fresh.common.util.CommonResult commonResult = new com.wx.fresh.common.util.CommonResult();
        commonResult.setData(data);
        commonResult.setCount(count);
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        return commonResult;
    }

    private static com.wx.fresh.common.util.CommonResult getLongModel(Integer code, String msg, Long total, Object data) {
        com.wx.fresh.common.util.CommonResult commonResult = new com.wx.fresh.common.util.CommonResult();
        commonResult.setData(data);
        commonResult.setTotal(total);
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        return commonResult;
    }

    /**
     * 返回Integer条数和 data
     *
     * @param count
     * @param data
     * @return
     */
    public static com.wx.fresh.common.util.CommonResult getModel(Integer count, Object data) {
        if (count > 0) {
            return getSingtenModel(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), count, data);
        } else {
            return getSingtenModel(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), count, data);
        }

    }

    /**
     * 返回Long类型的条数和 data
     *
     * @param total
     * @param data
     * @return
     */
    public static com.wx.fresh.common.util.CommonResult getModel(Long total, Object data) {
        if (total > 0) {
            return getLongModel(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), total, data);
        } else {
            return getLongModel(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), total, data);
        }

    }

    /**
     * 返回Integer的条数
     *
     * @param count
     * @return
     */
    public static com.wx.fresh.common.util.CommonResult getModel(Integer count) {
        if (count > 0) {
            return getSingtenModel(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), count, null);
        } else {
            return getSingtenModel(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), count, null);
        }

    }

    /**
     * 返回一个 data
     *
     * @param data
     * @return
     */
    public static com.wx.fresh.common.util.CommonResult getModel(Object data) {
        if (data != null) {
            return getSingtenModel(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), 1, data);
        } else {
            return getSingtenModel(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), 1, data);
        }
    }

    /**
     * 没有登录或者登录超时
     *
     * @return
     */
    public static com.wx.fresh.common.util.CommonResult noLogin() {
        return getSingtenModel(ResultCode.LOGIN_DATE.getCode(), ResultCode.LOGIN_DATE.getMsg(), ResultCode.LOGIN_DATE.getCount(), LOG_BACK_IN);
    }

    /**
     * 验证码错误
     *
     * @return
     */
    public static com.wx.fresh.common.util.CommonResult codeError() {
        return getSingtenModel(ResultCode.CODE_ERROR.getCode(), ResultCode.CODE_ERROR.getMsg(), ResultCode.CODE_ERROR.getCount(), LOG_BACK_IN);
    }


    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}